//
//  EditTaskProessViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/28.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  编辑流程主界面

#import "EditTaskProessViewController.h"
#import "CommonTableViewCell.h"
#import "SelectContactListViewController.h"

static NSString *CommonTableViewCellIdentifier=@"CommonTableViewCellIdentifier";

@interface EditTaskProessViewController ()<UITableViewDelegate,UITableViewDataSource,CuiPickViewDelegate,SelectContactListViewControllerDelegate>

@property(nonatomic,strong)UITableView *editTaskProessTableView;

@property(nonatomic,strong)NSString *endTimeStr;

@property(nonatomic,strong)UIView *headView;

@property(nonatomic,strong)UITextView *textView;

@property(nonatomic,strong)UITextField *nameDetailTF;


@property(nonatomic,strong)NSMutableArray *userNameArr;

@property(nonatomic,strong)NSMutableArray *userIDArr;

@property(nonatomic,assign)BOOL isChangeContact;

@property(nonatomic,assign)BOOL isChangeTime;

@property(nonatomic,strong)NSMutableArray *selectArr;

@end

@implementation EditTaskProessViewController

// delegate
-(void)selectContactUserNameArr:(NSMutableArray *)userNameArr userIDArr:(NSMutableArray *)userIDArr
{
    NSLog(@"sdhjfdhhfd---%@  ---%@   4444---%@",userNameArr[0],userIDArr,userNameArr);

    _isChangeContact=YES;
    self.userNameArr=userNameArr;
    self.userIDArr=userIDArr;
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [self.editTaskProessTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

-(UIView *)headView
{
    if (!_headView) {
        _headView=[UIView new];
        _headView=[[UIView alloc]initWithFrame:CGRectMake(0,64,APPW,0)];
        _headView.backgroundColor=WhiteColor;
        setViewHeight_Add(_headView,kmargin*2);

        // 流程标题
        UILabel *nameLabel=[self.headView getLabelWithText:@"流程标题" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.headView addSubview:nameLabel];
        nameLabel.frame=CGRectMake(kmargin*2,_headView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_headView,kmargin*2+kmargin/2);

        _nameDetailTF=[[UITextField alloc]initWithFrame:CGRectMake(kmargin*2,_headView.frame.size.height,APPW-kmargin*4,kmargin*3)];
        _nameDetailTF.font=Font_Number(16);
        _nameDetailTF.textColor=DarkGreyColor;
//        ViewBorderRadius(_nameDetailTF,kmargin/5,1,MAIN_BACK_COLOR);
        _nameDetailTF.placeholder=self.model.title;
        [self.headView addSubview:_nameDetailTF];
        setViewHeight_Add(_headView,kmargin*4);

        // 流程描述
        UILabel *projectDesLabel=[self.headView getLabelWithText:@"流程描述" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.headView addSubview:projectDesLabel];
        projectDesLabel.frame=CGRectMake(kmargin*2,_headView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_headView,kmargin*2);

        _textView=[[UITextView alloc]initWithFrame:CGRectMake(kmargin,_headView.frame.size.height,APPW-kmargin*2,APPH/8)];
        _textView.textColor=DarkGreyColor;
        _textView.font=Font_Number(16);
        NSLog(@"sdhjsdhjsd9---%@",self.model.descriptionStr);

        if (![self.model.descriptionStr isEqualToString:@""])
        {
         _textView.placeholder=self.model.descriptionStr;
        }
        else
        {
         _textView.placeholder=@"请输入流程描述";
        }

        [self.headView addSubview:_textView];
        setViewHeight_Add(_headView,APPH/8+kmargin);

        UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0,_headView.frame.size.height,APPW,kmargin)];
        lineView.backgroundColor=BGColor;
        [self.headView addSubview:lineView];
        setViewHeight_Add(_headView,kmargin);
    }
    return _headView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    _isChangeContact=NO;

    _isChangeTime=NO;

    self.selectArr=[NSMutableArray array];

    self.userNameArr=[NSMutableArray array];

    self.userIDArr=[NSMutableArray array];

    self.title=@"编辑流程";

    [AppUI initNavigationItem:self.navigationItem andWithText:@"确  认" andTextColor:WhiteColor andIsRight:YES withBlock:^{

        if ([_endTimeStr isEqualToString:@"请选择截止时间"])
        {
            [self.view makeToast:@"请选择截止时间" duration:.5 position:CSToastPositionCenter];
        }
        else
        {
            NSLog(@"sdhjdhjdfh9---%@ ----%@",self.model.step_id,self.model.estimate_etime);

            [self.selectArr removeAllObjects];

            NSString *userNameStr;
            if (_isChangeContact==NO)
            {
                NSLog(@"77799---%@",self.model.user_id);
                if ([self.model.user_id isEqualToString:@"0"])
                {
                    NSString *userID=self.model.old_user_ids[0];
                    NSString *userName=[self.model.old_user_names objectForKey:userID];
                    userNameStr=[NSString stringWithFormat:@"%@等",userName];
                    self.selectArr=self.model.old_user_ids;
                }
                else
                {
                    userNameStr=self.model.user_name;
                    [self.selectArr addObject:self.model.user_id];
                }

                NSLog(@"s333434343----%@",userNameStr);

            }
            else
            {
                [self.userNameArr componentsJoinedByString:@","];

                if (self.userNameArr.count>1)
                {
                    userNameStr=[NSString stringWithFormat:@"%@等",self.userNameArr[0]];
                    self.selectArr=self.userIDArr;
                }
                else
                {
                    userNameStr=self.userNameArr[0];
                    [self.selectArr addObject:self.userIDArr[0]];
                }

                NSLog(@"sddsjhdfhdf----%@",userNameStr);

            }

//            NSString *user=(_isChangeContact==NO)?self.model.user_id:self.userIDArr[0]
            [API EditTaskProgressWithStepID:self.model.step_id title:self.nameDetailTF.text userIDs:self.selectArr estimateEtime:(_isChangeTime==NO)?self.model.estimate_etime:self.endTimeStr fatherID:self.model.father_ids[0] remindMin:@"" description:self.textView.text attachments:@"" success:^(id res) {

                NSLog(@"sdhjdfh99--%@  -----%@" ,res,res[@"error"]);

                if ([res[@"rt"] isEqual:@(0)])
                {
                    [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];

                }
                else
                {
                     [self.view makeToast:@"修改成功" duration:1 position:CSToastPositionCenter];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                        [self.navigationController popViewControllerAnimated:NO];
                    });

                }
            } failure:^(NSInteger stateCode) {
                
            }];
        }
    }];

    [self.view addSubview:self.editTaskProessTableView];

}

// editTaskProessTableView
-(UITableView *)editTaskProessTableView
{
    if (!_editTaskProessTableView) {
        _editTaskProessTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH) style:UITableViewStylePlain];
        _editTaskProessTableView.dataSource=self;
        _editTaskProessTableView.delegate=self;
        _editTaskProessTableView.backgroundColor=MAIN_BACK_COLOR;
        _editTaskProessTableView.showsVerticalScrollIndicator=NO;
        _editTaskProessTableView.tableFooterView=[UIView new];
        _editTaskProessTableView.tableHeaderView=self.headView;
        _editTaskProessTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        [self.editTaskProessTableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:CommonTableViewCellIdentifier];
    }
    return _editTaskProessTableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        CommonTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CommonTableViewCellIdentifier];
        NSString *userNameStr;
        if (_isChangeContact==NO)
        {
            NSLog(@"77799---%@",self.model.user_id);
            if ([self.model.user_id isEqualToString:@"0"])
            {
                NSString *userID=self.model.old_user_ids[0];
                NSString *userName=[self.model.old_user_names objectForKey:userID];
                userNameStr=[NSString stringWithFormat:@"%@等",userName];
            }
            else
            {
                 userNameStr=self.model.user_name;
            }
        }
        else
        {
            [self.userNameArr componentsJoinedByString:@","];

            if (self.userNameArr.count>1)
            {
                userNameStr=[NSString stringWithFormat:@"%@等",self.userNameArr[0]];
            }
            else
            {
                userNameStr=self.userNameArr[0];
            }
        }
        [cell setCellInfoWithHeaderImage:@"" withTitle:@"负  责  人" withSubTitle:userNameStr withArrow:YES];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==1)
    {
        NSLog(@"67660---%@",self.model.estimate_stime);

        CommonTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CommonTableViewCellIdentifier];
        [cell setCellInfoWithHeaderImage:@"" withTitle:@"开始时间" withSubTitle:self.model.estimate_stime withArrow:NO];
        cell.arrowImageView.hidden=YES;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
//        NSString *timeStr;
//        if (self.endTimeStr==nil)
//        {
//          timeStr=@"请选择截止时间";
//        }
//        else
//        {
//            timeStr=self.endTimeStr;
//        }
        NSString *timeStr;
        if (_isChangeTime==NO)
        {
            timeStr=self.model.estimate_etime;
        }
        else
        {
            timeStr=self.endTimeStr;
        }

        CommonTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CommonTableViewCellIdentifier];
        [cell setCellInfoWithHeaderImage:@"" withTitle:@"截止时间" withSubTitle:timeStr withArrow:YES];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*5;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.row==0)
    {
        SelectContactListViewController *vc=[SelectContactListViewController new];
        vc.typeStr=@"EditTaskProgress";
        vc.delegate=self;
          NSMutableArray *arr=[NSMutableArray array];
        if ([self.model.user_id isEqualToString:@"0"])
        {
            arr=self.model.old_user_ids;
        }
        else
        {
            [arr addObject:self.model.user_id];
        }



        vc.selectContactIDArr=arr;

        NSLog(@"sdhjsdhhfd9----%@",vc.selectContactIDArr);

        [self.navigationController pushViewController:vc animated:NO];
    }
    else if(indexPath.row==2)
    {
       // 选择截止时间
        [self GetEndTime];
    }
}

// 结束时间
-(void)GetEndTime
{
    CuiPickerView *pickerView=[CuiPickerView new];
    pickerView.frame=CGRectMake(0,APPH,APPW,200);
    pickerView.delegate=self;
    pickerView.curDate=[NSDate date];
    [self.view addSubview:pickerView];
    [pickerView showInView:self.view];
}


// delegate 刷新数据
-(void)didFinishPickView:(NSString *)date
{
    self.endTimeStr=date;
    NSLog(@"sdhbhfdhfdh--%@",date);

    _isChangeTime=YES;
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:2 inSection:0];
    [_editTaskProessTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}


// 间隙置边
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
